package com.credithc.signature.manager.controller;

import com.credithc.baseserv.core.codes.CoreCodes;
import com.credithc.baseserv.core.message.Message;
import com.credithc.baseserv.core.message.MessageFactory;
import com.credithc.baseserv.pageHelper.PageInfo;
import com.credithc.signature.common.po.TemplateHistory;
import com.credithc.signature.common.ro.manager.TemplateRo;
import com.credithc.signature.common.service.TemplateService;
import com.credithc.signature.common.vo.manager.DictionaryVo;
import com.credithc.signature.common.vo.manager.TemplateVo;
import com.credithc.signature.manager.service.AuthorizationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描 述: 历史模板记录Controller<br/>
 * 作 者: JingBo Hu<br/>
 * 创 建：2019/5/24 16:55<br/>
 * 版 本：v1.0.0<br>
 */
@RestController
@RequestMapping("/manager/scs/v2")
@Slf4j
public class TemplateHistoryController {

    @Autowired
    MessageFactory messageFactory;

    @Autowired
    TemplateService templateService;

    @Autowired
    HttpServletResponse response;

    @Autowired
    AuthorizationService authorizationService;


    /**
     * 查询所有--历史模板
     *
     * @param templateRo
     * @return
     */
    @PostMapping("/template/history/list")
    public Message queryAll(@RequestHeader("token") String token, @RequestBody TemplateRo templateRo, HttpServletRequest request) {
        try {
            TemplateRo newTemplateRo = authorizationService.authTemplate(templateRo, token, request);
            PageInfo<TemplateVo> pageInfo = templateService.queryAllHistory(newTemplateRo);
            Map<String, PageInfo<TemplateVo>> map = new HashMap<>(16);
            map.put("pageInfo", pageInfo);
            return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
        } catch (Exception e) {
            log.error("获取授权时发生异常", e);
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "获取授权时发生异常");
        }
    }

    /**
     * 查询单条
     *
     * @param id
     * @return
     */
    @GetMapping("/template/history/one/{id}")
    public Message queryOne(@PathVariable Long id) {
        TemplateVo templateVo = templateService.queryOneHistory(id);
        Map<String, Object> map = new HashMap<>(16);
        map.put("vo", templateVo);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
    }

    /**
     * 查询公章规则号--根据模板ID
     *
     * @return
     */
    @PostMapping("/template/history/officialSealId")
    public Message queryOfficialSealById(@RequestBody TemplateRo templateRo) {
        List<DictionaryVo> vos = templateService.queryHistoryOfficialSealById(templateRo);
        Map<String, Object> map = new HashMap<>(16);
        map.put("list", vos);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
    }

    /**
     * 导出文件
     *
     * @param ids
     */
    @GetMapping("/template/history/export/{ids}")
    public void export(@PathVariable String ids) {
        templateService.export(ids, response, TemplateHistory.class.getSimpleName());
    }
}
